/**
 * Created by Tuffy on 16/1/29.
 */
'use strict';

angular.module('starter.controllers')
.controller('DistributionOrderCtrl', ['$scope','$rootScope', '$timeout',  'ReportDistributionOrderService', 'ionicMaterialMotion', 'ionicMaterialInk','$state','$cordovaToast',
    function($scope,$rootScope, $timeout, ReportDistributionOrderService, ionicMaterialMotion, ionicMaterialInk,$state,$cordovaToast) {

    $scope.distributionOrder = {
        search: {
            value:'',
            filter:{},
            doAct:function(){
                if(this.timer){
                    $timeout.cancel(this.timer);
                }
                this.timer = $timeout(function(){
                    $scope.distributionOrder.refresh();
                },500);
            }
        },
        page: {
            pageNum:0,
            pageSize:10,
            total:0
        },
        loaded:false,
        data:[],
        loadData:function(okFunc){
            var _this = this,
                params = {
                    hotelName:this.search.value,
                    pageNum:_this.page.pageNum,
                    pageSize:_this.page.pageSize
                };
            ReportDistributionOrderService.findDistributionOrderList(params)
                .success(function(res){
                    if(res.success){
                        if(typeof (okFunc) == 'function'){
                            okFunc();
                        }
                        var _data = res.attribute.rows;
                        if(!_data || _data.length<=0){
                            _this.moreDataCanBeLoaded = false;
                            $cordovaToast.showShortBottom('已加载到尾页');
                            return;
                        }else if(_data.length==_this.page.pageSize){
                            _this.moreDataCanBeLoaded = true;
                        }
                        _this.data = _this.data.concat(_data);
                    }else{
                        console.log('加载失败');
                    }
                }).error(function(error){
                console.log(error);
            }).finally(function(){
                _this.loaded = true;
                if(_this.data.length<_this.page.pageSize){
                    _this.moreDataCanBeLoaded = false;
                }
                $scope.$broadcast('scroll.refreshComplete');
                $scope.$broadcast('scroll.infiniteScrollComplete');
                $timeout(function() {
                    ionicMaterialMotion.fadeSlideIn();
                }, 200);

                $timeout(function() {
                    ionicMaterialInk.displayEffect();
                }, 1000);
            });
        },
        moreDataCanBeLoaded:true,
        loadMore:function(){
            this.page.pageNum++;
            this.loadData();
        },
        refresh:function(){
            var _this = this;
            _this.page.pageNum = 1;
            _this.loadData(function(){
                _this.data = [];
            });
        }
    } ;

    $scope.distributionOrder.loadMore();

    $scope.goDetailMain = function(distr){
        // 详细信息
        $state.go('app.chars-distribution-order-detail', {orderId: distr.orderId});
    }
}])
.controller('DistributionOrderDetailCtrl', ['$scope','$rootScope', '$timeout',  'ReportDistributionOrderService', 'ionicMaterialMotion', 'ionicMaterialInk','$state',
    function($scope,$rootScope, $timeout, ReportDistributionOrderService, ionicMaterialMotion, ionicMaterialInk,$state) {
        var orderId =  $state.params.orderId;

        $scope.orderDetail = {
               /* hotelName:"测试酒店",
                channel:"去哪",
                orderName:"李雪楠",
                orderNamePhone:"13301360605",
                orderId:"1234567",
                amout:"1000.00",
                orderStatus:"已下单",
                payType:"到付",
                orderTime:"2016-02-19 23:09"*/
        }

        var params = {
            orderId:orderId
        };
        ReportDistributionOrderService.findOrderDetailByOrderId(params)
            .success(function(res){
                if(res.success){
                    $scope.orderDetail = res.attribute;
                }else{
                    console.log(res.errorMsg);
                }
            }).error(function(error){
                console.log(error);
            })
}])
.service('ReportDistributionOrderService',['CrmHttp','$rootScope',function(CrmHttp,$rootScope){
    var url = '/crm/crmHotel/';
    return {
        /**
         * 分销酒店全部数据
         * @param params
         * @returns {*}
         */
        findDistributionOrderList: function (params) {
            var act = 'findDistributionList';
            params['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, params);
        },
        /**
         * 分销酒店全部数据
         * @param params
         * @returns {*}
         */
        findOrderDetailByOrderId: function (params) {
            var act = 'findOrderDetailByOrderId';
            return CrmHttp.post(url + act, params);
        }
    };
}]);